import type { Router } from "vue-router";
import { useAuthStore } from "@/store/modules/auth";
import { useRouterStore } from "@/store/modules/router";

/**
 * Router guard
 *
 * @param router - Router instance
 */
export function createRouterGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    // const authStore = useAuthStore();
    // const routerStore = useRouterStore();
    // if (from.path === "/") {
    //   await authStore.getUserInfoAction();
    // }

    // if (from.path === "/") {
    //   [...routerStore.authRoutes].forEach(route => {
    //     router.addRoute(route);
    //   });
    // }

    // console.log(to);

    // const routes = router.getRoutes();
    // const toMatched = routes.find(item => item.path === to.path);

    // if (to?.meta?.constant) {
    //   // 静态路由
    //   next();
    // } else {
    //   // token鉴权
    //   if (authStore.token) {
    //     next({ ...to, replace: true });
    //   } else {
    //     next("/login");
    //   }
    // }
    next();
  });
}
